<template>
	<div class="app-container">
		<el-form :model="queryWbu" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="55px">
			<el-form-item label="小区" prop="communityId">
				<el-select v-model="queryWbu.communityId" placeholder="请选择小区" @keyup.enter.native="handleQuery">
					<el-option v-for="item in communityList" :key="item.communityId" :label="item.communityName"
						:value="item.communityId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="楼座" prop="balconyId">
				<el-select v-model="queryWbu.balconyId" placeholder="请选择楼座" @keyup.enter.native="handleQuery">
					<el-option v-for="item in balconyList" :key="item.balconyId" :label="item.balconyName"
						:value="item.balconyId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="单元" prop="unitId">
				<el-select v-model="queryWbu.unitId" placeholder="请选择单元" @keyup.enter.native="handleQuery">
					<el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item.unitId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="房号" prop="homeId">
				<el-select v-model="queryWbu.homeId" placeholder="请选择房号" @keyup.enter.native="handleQuery">
					<el-option v-for="item in homeList" :key="item.homeId" :label="item.homeNumber"
						:value="item.homeId">
					</el-option>
				</el-select>
			</el-form-item>

			<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
			<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['charge:allocation:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['charge:allocation:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['charge:allocation:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['charge:allocation:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="allocationList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="编号" align="center" prop="allocationId" />
			<el-table-column label="小区" align="center" prop="wyBalconyunithomeVo.communityName" />
			<el-table-column label="楼座" align="center" prop="wyBalconyunithomeVo.balconyName" />
			<el-table-column label="单元" align="center" prop="wyBalconyunithomeVo.unitName" />
			<el-table-column label="房间号" align="center" prop="wyBalconyunithomeVo.homeNumber" />
			<el-table-column label="费用" align="center" prop="wyCost.costName" />
			<el-table-column label="状态" align="center" prop="alocationStatus">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.alocationStatus" />
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['charge:allocation:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['charge:allocation:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改费用分配对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="小区" prop="balconyunithomeId">
					<el-select v-model="queryWbu.communityId" v-bind:disabled='isDisabled' placeholder="请选择小区"
						@keyup.enter.native="handleQuery">
						<el-option v-for="item in communityList" :key="item.communityId" :label="item.communityName"
							:value="item.communityId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="楼座" prop="balconyunithomeId">
					<el-select v-model="queryWbu.balconyId" v-bind:disabled='isDisabled' placeholder="请选择楼座"
						@keyup.enter.native="handleQuery">
						<el-option v-for="item in balconyList" :key="item.balconyId" :label="item.balconyName"
							:value="item.balconyId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="单元" prop="balconyunithomeId">
					<el-select v-model="queryWbu.unitId" v-bind:disabled='isDisabled' placeholder="请选择单元"
						@keyup.enter.native="handleQuery">
						<el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName"
							:value="item.unitId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="房号" prop="balconyunithomeId">
					<el-select v-model="queryWbu.homeId" v-bind:disabled='isDisabled' placeholder="请选择房号"
						@keyup.enter.native="handleQuery">
						<el-option v-for="item in homeList" :key="item.homeId" :label="item.homeNumber"
							:value="item.homeId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="费用类型" prop="costId">
					<el-select v-model="form.costId" placeholder="请选择收费类型" @keyup.enter.native="handleQuery">
						<el-option v-for="item in costList" :key="item.costId" :label="item.costName"
							:value="item.costId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="状态" prop="alocationStatus">
					<el-radio-group v-model="form.alocationStatus">
						<el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
							:label="dict.value">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listAllocation,
		getAllocation,
		delAllocation,
		addAllocation,
		updateAllocation,
		getCommunity,
		getBalconyUnitHome,
	} from "@/api/charge/allocation";
	import {
		listCost
	} from "@/api/charge/cost";

	export default {
		name: "Allocation",
		dicts: ['sys_normal_disable'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 费用分配表格数据
				allocationList: [],
				// 小区列表数据
				communityList: [],
				// 楼座列表数据
				balconyList: [],
				// 单元列表数据
				unitList: [],
				// 房间列表数据
				homeList: [],
				// 费用列表
				costList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					communicateId: null,
					balconyunithomeIds: []
				},
				queryWbu: {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				},
				// 表单参数
				form: {
					communicateId: null,
					balconyunithomeId: null
				},
				// 监控是否清除中间表数据
				cleanWbu: true,
				// 表单校验
				rules: {
					balconyunithomeId: [{
						required: true,
						message: '请选择位置信息',
						trigger: 'blur'
					}]
				},
				// 是否可以修改位置信息
				isDisabled: true
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询费用分配列表 */
			getList() {
				this.loading = true;
				listAllocation(this.queryParams).then(response => {
					this.allocationList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					allocationId: null,
					communicateId: null,
					balconyunithomeId: null,
					costId: null,
					alocationStatus: null,
					del: null
				};
				this.queryWbu = {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				};
				this.queryParams = {
					pageNum: 1,
					pageSize: 10,
					communicateId: null,
					balconyunithomeIds: []
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.queryParams = {
						pageNum: 1,
						pageSize: 10,
						communicateId: null,
						balconyunithomeIds: []
					},
					this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.allocationId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.isDisabled = false
				this.open = true;
				this.title = "添加费用分配";

			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				// const allocationId = row.allocationId || this.ids
				// getAllocation(allocationId).then(response => {
				// 	this.form = response.data;
				// 	this.open = true;
				// 	this.title = "修改费用分配";
				// });
				this.isDisabled = true
				this.cleanWbu = false
				this.queryWbu = {
					communityId: row.wyBalconyunithomeVo.communityId,
					balconyId: row.wyBalconyunithomeVo.balconyId,
					unitId: row.wyBalconyunithomeVo.unitId,
					homeId: row.wyBalconyunithomeVo.homeId
				}
				this.form = {
					allocationId: row.allocationId,
					communicateId: row.wyBalconyunithomeVo.communityId,
					balconyunithomeId: row.wyBalconyunithomeVo.id,
					costId: row.wyCost.costId,
					alocationStatus: row.alocationStatus,
				}
				this.open = true;
				this.title = "修改费用分配";
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.allocationId != null) {
							updateAllocation(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addAllocation(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const allocationIds = row.allocationId || this.ids;
				this.$modal.confirm('是否确认删除费用分配编号为"' + allocationIds + '"的数据项？').then(function() {
					return delAllocation(allocationIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('charge/allocation/export', {
					...this.queryParams
				}, `allocation_${new Date().getTime()}.xlsx`)
			},
			/* 获取小区列表 */
			getCommunityList() {
				getCommunity().then(res => {
					this.communityList = res.data
				})
			},
			/* 获取费用列表 */
			getCostList() {
				listCost().then(res => {
					this.costList = res.rows
				})
			}

		},
		mounted() {
			this.getCommunityList()
			this.getCostList()

		},
		watch: {
			'queryWbu.communityId': {
				handler() {

					if (this.cleanWbu) {
						this.balconyList = []
						this.unitList = []
						this.homeList = []
						this.queryWbu.balconyId = null
						this.queryWbu.unitId = null
						this.queryWbu.homeId = null
						this.form.balconyunithomeId = null
					}
					this.queryParams.balconyunithomeIds = null
					this.queryParams.communicateId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.balconyList = res.data[0]
						if (res.data.length > 3) {
							this.queryParams.balconyunithomeIds = res.data[3]
						}
						this.queryParams.communicateId = this.queryWbu.communityId
						this.form.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.balconyId': {
				handler() {

					if (this.cleanWbu) {
						this.unitList = []
						this.homeList = []
						this.queryWbu.unitId = null
						this.queryWbu.homeId = null
						this.form.balconyunithomeId = null
					}
					this.queryParams.balconyunithomeIds = null
					this.queryParams.communicateId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.unitList = res.data[1]
						if (res.data.length > 3) {
							this.queryParams.balconyunithomeIds = res.data[3]
						}
						this.queryParams.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.unitId': {
				handler() {
					if (this.cleanWbu) {
						this.homeList = []
						this.queryWbu.homeId = null
						this.form.balconyunithomeId = null
					}
					this.queryParams.balconyunithomeIds = null
					this.queryParams.communicateId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.homeList = res.data[2]
						if (res.data.length > 3) {
							this.queryParams.balconyunithomeIds = res.data[3]
						}
						this.queryParams.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.homeId': {
				handler() {
					this.queryParams.balconyunithomeIds = null
					this.queryParams.communicateId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						if (res.data.length > 3) {
							this.queryParams.balconyunithomeIds = res.data[3]
							this.form.balconyunithomeId = res.data[3][0]
						}
						this.queryParams.communicateId = this.queryWbu.communityId
						this.cleanWbu = true
					})
				},
				deep: true,
				immediate: true
			}
		}
	};
</script>